//
//  PGUserAccount.swift
//  PGWeibo
//
//  Created by Apple on 15/11/6.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class PGUserAccount: NSObject, NSCoding {
    /// 令牌
    var access_token: String?
    /// 过期时间
    private var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
     /// uid用户ID
     var uid: Int64 = 0
     /// 过期日期
     var expiresDate: NSDate?
    /// 用户头像图片 (大图)
    var avatar_large: String?
    /// 用户名称
    var name: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    ///description:
    override var description: String {
        
        let keys = ["access_token", "expiresDate", "uid", "avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    /// 解档
    ///
    /// - parameter aDecoder: 解档器
    ///
    /// - returns: 对象
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeInt64ForKey("uid")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        
    }
    /// 归档
    ///
    /// - parameter aCoder: 归档器
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeInt64(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(name, forKey: "name")
    }
}
